﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HttpRequester;
using mshtml;
using Common.Constants;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.RequestChainItems
{
    [ChainItem]
    public class WorkInKarierAgaineChainItem : BotvaLogedInChainItemBase
    {
        protected override bool IsContextValidInternal()
        {
            return base.IsContextValidInternal();
        }

        protected override void PrepareRequestInternal()
        {
            Url = "http://g3.botva.ru";
            Method = HttpMethod.GET;

            string content = Chain.LastContent;
            content = content.Replace(Environment.NewLine, string.Empty);
            while (content.IndexOf("  ") >= 0)
            {
                content = content.Replace("  ", " ");
            }
            content = content.ToUpper();

            IHTMLDocument2 html = new HTMLDocumentClass();
            html.write(content);
            html.close();

            IHTMLElementCollection all = html.body.all as IHTMLElementCollection;
            IHTMLElementCollection ahrefs = all.tags("A") as IHTMLElementCollection;


            //string s = "";
            foreach (IHTMLElement item in ahrefs)
            {
                if (item.innerText != "ИСКАТЬ ДАЛЬШЕ")
                    continue;

                IHTMLAnchorElement ahref = item as IHTMLAnchorElement;

                string address = ahref.pathname;
                if (!string.IsNullOrEmpty(address))
                    Url += "/" + address.ToLower();

                string queryString = ahref.search;
                if (queryString.StartsWith("?"))
                    queryString = queryString.Substring("?".Length);
                string[] parameters = queryString.Split('&');
                foreach (string param in parameters)
                {
                    string[] paramParts = param.Split('=');
                    if (paramParts.Length >= 2)
                    {
                        AddGet(paramParts[0].ToLower(), paramParts[1].ToLower());
                    }
                    else
                    {
                        AddGet(paramParts[0].ToLower(), string.Empty);
                    }
                }

                break;
            }

            //rm.AddPost("submit", "ОТПРАВИТЬ");

            ////txtContent.Text = s;

            //MakeRequest(rm); 



            //Url = "http://g3.botva.ru";
            //Method = HttpMethod.GET;

            //string content = Chain.LastContent;
            //content = content.Replace(Environment.NewLine, string.Empty);
            //while (content.IndexOf("  ") >= 0)
            //{
            //    content = content.Replace("  ", " ");
            //}
            //content = content.ToUpper();

            //IHTMLDocument2 html = new HTMLDocumentClass();
            //html.write(content);
            //html.close();

            //IHTMLElementCollection all = html.body.all as IHTMLElementCollection;
            //IHTMLElementCollection ahrefs = all.tags("A") as IHTMLElementCollection;

            //foreach (IHTMLElement item in ahrefs)
            //{
            //    if (item.innerText != "ДОБЫТЬ КРИСТАЛЛ")
            //        continue;

            //    IHTMLAnchorElement ahref = item as IHTMLAnchorElement;

            //    string address = ahref.pathname;
            //    if (!string.IsNullOrEmpty(address))
            //        Url += "/" + address.ToLower();

            //    string queryString = ahref.search;
            //    if (queryString.StartsWith("?"))
            //        queryString = queryString.Substring("?".Length);
            //    string[] parameters = queryString.Split('&');
            //    foreach (string param in parameters)
            //    {
            //        string[] paramParts = param.Split('=');
            //        if (paramParts.Length >= 2)
            //        {
            //            AddGet(paramParts[0].ToLower(), paramParts[1].ToLower());
            //        }
            //        else
            //        {
            //            AddGet(paramParts[0].ToLower(), string.Empty);
            //        }
            //    }

            //    break;
            //}
        }

        public override bool IsResponseValidInternal()
        {
            //string content = Requester.PageContent;
            //content = content.Replace(" ", string.Empty)
            //                .Replace("'", "\"")
            //                .ToUpper();

            //string searchedStr = "<input type='submit' class='cmd_all cmd_medium_sl cmd_amedium_sl ' value='ВХОД' >";
            //searchedStr = searchedStr.Replace(" ", string.Empty)
            //                .Replace("'", "\"")
            //                .ToUpper();

            //return content.Contains(searchedStr);

            return true;
        }
    }
}
